/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package G3.Mod;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import java.io.IOException;

/**
 *
 * @author Dymion
 */
public class overclock extends Activity implements OnClickListener {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.overclock);
        warning("Before continuing, make sure you have set \"PERFORMANCE\" governor on and min and max frequencies to the maximum.");
        View ocBtn = findViewById(R.id.ovBtn);
        ocBtn.setOnClickListener(this);
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ovBtn:
                if (readValue() <= 1400) {
                    overclock(calculate(readValue()));
                } else {
                    warning("Due to safety reasons we restricted overclocking to a max value of 1400.");
                }
                break;
        }
    }

    public int readValue() {
        EditText overclockValue = (EditText) findViewById(R.id.ovInput);
        return Integer.parseInt(overclockValue.getText().toString());
    }

    public String calculate(int freq) {
        String value = null;
        value = Integer.toHexString(freq / 2);
        return value;
    }

    public void warning(String message) {
        DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                    case DialogInterface.BUTTON_POSITIVE:
                        break;
                }
            }
        };

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(message).setPositiveButton("Ok", dialogClickListener).show();
        builder.setCancelable(false);
    }

    public void overclock(String value) {
        TextView result = (TextView) findViewById(R.id.ovResultTxt);
        try {
            Runtime.getRuntime().exec(new String[]{"/system/bin/su", "-c", "echo \"0 a" + value + "0301 a14d0303 c07e13fc cf850240\" > /sys/devices/system/cpu/cpu0/cpufreq/plls_table"});
            result.setText("You have overclocked with " + Integer.parseInt(value, 16) * 2);
        } catch (IOException ex) {
            //It doesn't work..
        }
    }
}
